package com.lightkits.wms.domain.deliveryorder;

import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.database.dao.WmsCustomerItemMapper;
import com.lightkits.wms.database.model.CustomerItemModel;
import com.lightkits.wms.dto.deliveryorder.QueryCustomerByDoRequestType;
import com.lightkits.wms.dto.deliveryorder.QueryCustomerByDoResponseType;
import com.lightkits.wms.dto.deliveryorder.model.CustomerItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * QueryCustomerItemExecutor.
 *
 * @author jing.wei@hand-china.com.
 * Created on 2019/2/27 15:58.
 */
@Component
public class QueryCustomerItemExecutor implements ServiceExecutor
        <QueryCustomerByDoRequestType, QueryCustomerByDoResponseType> {
    private final WmsCustomerItemMapper fndCustomerItemMapper;

    @Autowired
    public QueryCustomerItemExecutor(WmsCustomerItemMapper fndCustomerItemMapper) {
        this.fndCustomerItemMapper = fndCustomerItemMapper;
    }

    @Override
    public QueryCustomerByDoResponseType service(QueryCustomerByDoRequestType request) {
        QueryCustomerByDoResponseType response = new QueryCustomerByDoResponseType();
        response.setSuccessful(true);
//         if(ObjectUtil.isNullOrEmpty(request.getDoItemId())){
//             response.setMessage("Do Item Id is null");
//             response.setSuccessful(false);
//             return response;
//         }
        List<CustomerItemModel> models = fndCustomerItemMapper
                .queryCustomerItemByDo(request.getDoItemId(), request.getCustomerNameOrNum());

        List<CustomerItemDTO> customerItemDTOS = new ArrayList<>();
        models.forEach(target -> {
            CustomerItemDTO dto = new CustomerItemDTO();
            dto.setCustomerId(target.getCustomerId());
            dto.setCustomerItem(target.getCustomerItem());
            dto.setCustomerName(target.getCustomerName());
            dto.setCustomerNum(target.getCustomerNum());
            customerItemDTOS.add(dto);
        });

        response.setCustomerItemDTOS(customerItemDTOS);
        return response;
    }
}
